package com.wgl.express.UI;

import com.wgl.express.service.Impl.UserServiceImpl;
import com.wgl.express.service.UserService;
import com.wgl.express.util.Util;

import static com.wgl.express.text.Text.*;

public class Login {
    public static int loginStatus;
    private String username;
    private String password;
    private Util util;

    public void start(){
        util=  Util.getUtil();


        eventLoop();
    }

    private void eventLoop(){
        boolean exit = false;
        util.say(TITLE);
        util.say(MENU_LOGIN);
        while (!exit){

            util.readLine();
            util.say("请输入用户名：");
            this.username = util.readLine();
            util.say("请输入密码：");
            this.password = util.readLine();
            if (login()){
                util.say("登录成功！\n\n");
                exit = true;
            }else{
                util.say("用户名或密码错误！请重试!\n");
                util.say("返回请输入0，继续请输入1:");
                if (util.readInt() == 0){
                    return;
                }
            }
        }

        switch (loginStatus){
            case 1:gotoUser();break;
            case 2:gotoAdmin();break;
            case 3:gotoDeliver();break;
        }
    }

    private void gotoDeliver() {
        Deliver deliver  =new Deliver();
        deliver.start();
    }

    private void gotoUser() {
        Mine mine = new Mine();
        mine.start();
    }

    private void gotoAdmin() {
        Admin admin = new Admin();
        admin.start();

    }

    private boolean login(){
        UserService userService = new UserServiceImpl();
        loginStatus = userService.login(username,password);
        return loginStatus>0;
    }

}
